package com.dhy.commadstate.util;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommandSettings extends AndroidBasicSerializable<CommandSettings> {

    private Map<String, String> list = new HashMap<>();

    /**
     * ExternalStorageDirectory
     */
    public CommandSettings(Context context, String fileName) {
        super(context, getFilePath(fileName));
    }

    public List<MyCommand> getCommands() {
        List<MyCommand> r = new ArrayList<>();
        for (String s : list.values()) {
            r.add(new MyCommand(s));
        }
        return r;
    }

    @Override
    public CommandSettings load() {
        load(this);
        return this;
    }

    private static final String FOLDER = "CommadState";

    public static String getFilePath(String name) {
        return Environment.getExternalStorageDirectory() + File.separator + FOLDER + File.separator + name;
    }

    public static File getSettingFolder() {
        return new File(Environment.getExternalStorageDirectory(), FOLDER);
    }

    public boolean update(MyCommand command) {
        list.put(command.name, command.toString());
        return false;
    }

    public boolean save(MyCommand command) {
        list.put(command.name, command.toString());
        return super.save();
    }

    public void clear() {
        list.clear();
    }
}
